{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append('../utils')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from aoc import * # get_input, d4, d8, d4n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "YEAR = 2021\n",
    "DAY = 24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "inp = get_input(YEAR, DAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def do_transform(inp):\n",
    "    return list(filter(lambda x: len(x) > 0, inp.split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "inp = do_transform(inp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def solve(inp, order):\n",
    "    CMD_LEN = 18\n",
    "    X_OFFSET = 5\n",
    "    Y_OFFSET = 15\n",
    "    Z_OFFSET = 4\n",
    "    \n",
    "    n = len(inp) // CMD_LEN\n",
    "    a = [int(inp[i * CMD_LEN + X_OFFSET].split()[2]) for i in range(n)]\n",
    "    b = [int(inp[i * CMD_LEN + Y_OFFSET].split()[2]) for i in range(n)]\n",
    "    c = [int(inp[i * CMD_LEN + Z_OFFSET].split()[2] == '26') for i in range(n)]\n",
    "            \n",
    "    x = [0]\n",
    "    z = [0]\n",
    "    ans = []\n",
    "    \n",
    "    \n",
    "    def dfs():\n",
    "        i = len(ans)\n",
    "        \n",
    "        if i == n:\n",
    "            return z[0] == 0\n",
    "        \n",
    "        for val in order:\n",
    "            ox = x[0]\n",
    "            oz = z[0]\n",
    "                   \n",
    "            x[0] = 1 if (z[0] % 26 + a[i]) != val else 0\n",
    "            if a[i] < 0 and x[0] == 1:\n",
    "                continue\n",
    "\n",
    "            if c[i] == 1:\n",
    "                z[0] //= 26\n",
    "\n",
    "            z[0] = (25 * x[0] + 1) * z[0] + (val + b[i]) * x[0]\n",
    "            ans.append(val)\n",
    "            \n",
    "            if dfs():\n",
    "                return True\n",
    "            \n",
    "            ans.pop()\n",
    "            \n",
    "            x[0] = ox\n",
    "            z[0] = oz\n",
    "            \n",
    "        return False\n",
    "    \n",
    "    \n",
    "    dfs()\n",
    "    \n",
    "    return int(''.join(map(str, ans)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part1(inp):\n",
    "    return solve(inp, range(9, 0, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "59692994994998"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1_ans = part1(inp)\n",
    "part1_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part1_ans, YEAR, DAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part2(inp):\n",
    "    return solve(inp, range(1, 10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16181111641521"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part2_ans = part2(inp)\n",
    "part2_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part2_ans, YEAR, DAY, level=2)"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
